<template>
	<view class="uni-navbar">
		<view :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }" :style="{ 'background-color': backgroundColor }"
		 class="uni-navbar__content">
			<uni-status-bar v-if="statusBar" />
			<view :style="{ color: color,backgroundColor: backgroundColor }" class="uni-navbar__header uni-navbar__content_view">
				<view @tap="onClickLeft" class="uni-navbar__header-btns uni-navbar__header-btns-left uni-navbar__content_view">
					<view v-if="firstPage" class="uni-navbar__content_view">
						<view class="homeIcon-wrap">
							<image src="/static/common/home_icon.png" class="homeIcon"></image>
						</view>
					</view>
					<block v-else>
						<view class="uni-navbar__content_view" v-if="leftIcon.length"> 
							<uni-icons :color="color" :type="leftIcon" size="24" /> 
						</view>
						<view :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length }" class="uni-navbar-btn-text uni-navbar__content_view"
						 v-if="leftText.length">
							<text :style="{ color: color, fontSize: '14px' }">{{ leftText }}</text>
						</view>
						<slot name="left" />
					</block>
				</view>
				<view class="uni-navbar__header-container uni-navbar__content_view">
					<view class="uni-navbar__header-container-inner uni-navbar__content_view" :class="{'title-left':titleLeft}" v-if="title.length">
						<text class="uni-nav-bar-text" :style="{color: color }">{{ title }}</text>
					</view>
					<!-- 标题插槽 -->
					<slot />
				</view>
				<view :class="title.length ? 'uni-navbar__header-btns-right' : ''" :style="{width:firstPage||leftIcon?'80rpx':'20rpx' }" @tap="onClickRight" class="uni-navbar__header-btns uni-navbar__content_view">
					<view class="uni-navbar__content_view" v-if="rightIcon.length">
						<uni-icons :color="color" :type="rightIcon" size="24" />
					</view>
					<!-- 优先显示图标 -->
					<view class="uni-navbar-btn-text uni-navbar__content_view" v-if="rightText.length && !rightIcon.length">
						<text class="uni-nav-bar-right-text">{{ rightText }}</text>
					</view>
					<slot name="right" />
				</view>
			</view>
		</view>
		<view class="uni-navbar__placeholder" v-if="fixed">
			<uni-status-bar v-if="statusBar" />
			<view class="uni-navbar__placeholder-view" />
		</view>
	</view>
</template>

<script>
	import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
	import uniIcons from "../uni-icons/uni-icons.vue";
	// 主页页面的页面路径
	// 关联功能：打开的页面只有一个的时候右上角自动显示返回首页按钮，下面这个数组是排除显示返回首页的页面。
	// 主页使用场景：小程序分享出去的页面，用户点击开是分享页面，很多情况下是没有返回首页按钮的
	const mainPagePath = ["pages/index/index","pages/user/user","pages/shop/index"];
	//返回首页的地址
	const homePath = "/pages/index/index";
	export default {
		name: "UniNavBar",
		components: {
			uniStatusBar,
			uniIcons
		},
		props: {
			isLeftEvent: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: ""
			},
			titleLeft:{
				type: Boolean,
				default: false
			},
			leftText: {
				type: String,
				default: ""
			},
			rightText: {
				type: String,
				default: ""
			},
			leftIcon: {
				type: String,
				default: ""
			},
			rightIcon: {
				type: String,
				default: ""
			},
			fixed: {
				type: [Boolean, String],
				default: false
			},
			color: {
				type: String,
				default: "#000000"
			},
			backgroundColor: {
				type: String,
				default: "#FFFFFF"
			},
			statusBar: {
				type: [Boolean, String],
				default: false
			},
			shadow: {
				type: [String, Boolean],
				default: false
			},
			border: {
				type: [String, Boolean],
				default: false
			}
		},
		data() {
			return {
				//当前页面是否是第一个页面
				firstPage: false
			}
		},
		//第一次加载
		created() {
			let currentPages = getCurrentPages();
			let pageLen = currentPages.length;
			//判断是否是第一个页面，如果是有设置back为true的页面，将不显示返回箭头，而现实返回首页按钮
			if(homePath == currentPages[0].route){
				this.firstPage = false;
			}	
			else if (pageLen == 1 && !mainPagePath.includes(currentPages[0].route)) {
				this.firstPage = true;
			}
		},
        mounted() {
          if(uni.report && this.title !== '') {
              uni.report('title', this.title)
          }
        },
		methods: {
			onClickLeft() {
				if(this.isLeftEvent == false){
					if(this.firstPage == true){
						this.$Router.reLaunch(homePath)
					}else{
						this.$Router.navigateBack()
					}
				}else{
					this.$emit("clickLeft");
				}
			},
			onClickRight() {
				this.$emit("clickRight");
			}
		}
	};
</script>

<style lang="scss" scoped>
	/* #ifndef MP-WEIXIN */
	$nav-height: 41px;
	/* #endif */
	/* #ifndef H5 */
	$nav-height: 44px;
	/* #endif */
	.uni-nav-bar-text {
		
		/* #ifdef APP-PLUS */
		font-size: 36rpx;
		/* #endif */
		/* #ifndef APP-PLUS */
		font-size: 36rpx;
		/* #endif */
		font-size: 36rpx;
	}
	.uni-nav-bar-right-text {
		font-size: 12+$uni-font-size-base;
	}

	.uni-navbar {
		width: 750rpx;
	}

	.uni-navbar__content {
		position: relative;
		width: 750rpx;
		background-color: $uni-bg-color;
		overflow: hidden;
	}

	.uni-navbar__content_view {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		flex-direction: row;
		// background-color: #FFFFFF;
	}
	
	.uni-navbar__header {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		width: 750rpx;
		height: $nav-height;
		line-height: $nav-height;
		font-size: 12+16px;
		// background-color: #ffffff;
	}

	.uni-navbar__header-btns {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-wrap: nowrap;
		padding: 0 20rpx;
		justify-content: center;
		align-items: center;
	}

	.uni-navbar__header-btns-left {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: flex-start;
	}

	.uni-navbar__header-btns-right {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 80rpx;
		padding-right: 30rpx;
		justify-content: flex-end;
	}

	.uni-navbar__header-container {
		flex: 1;
	}

	.uni-navbar__header-container-inner {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		align-items: center;
		justify-content: center;
		font-size: 12+$uni-font-size-base;
		&.title-left{
			justify-content: flex-start;
		}
	}


	.uni-navbar__placeholder-view {
		height: $nav-height;
	}

	.uni-navbar--fixed {
		position: fixed;
		z-index: 998;
	}

	.uni-navbar--shadow {
		/* #ifndef APP-NVUE */
		box-shadow: 0 1px 6px #ccc;
		/* #endif */
	}

	.uni-navbar--border {
		border-bottom-width: 1rpx;
		border-bottom-style: solid;
		border-bottom-color: $uni-border-color;
	}
	.homeIcon-wrap{
		background-color: rgba(0,0,0,0.3);
		padding: 12rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius:50rpx;
		.homeIcon{
			width: 32rpx;
			height: 32rpx;
		}
	}
	.arrowlive{
		width: 64rpx;
		height: 64rpx;
	}
</style>
